package com.lapian.designmode.observer.mashibing.v7;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建日期：2021/11/15 10:16
 * 加入多个观察者
 *
 * @author tony.sun
 * 类说明：
 */
class Child {
    private boolean cry = false;
    private List<MyObserver> observers=new ArrayList<>();
    {
        observers.add(new Dad());
        observers.add(new Mum());
        observers.add(new Dog());
    }

    public boolean isCry() {
        return cry;
    }

    public void wakeUp() {
        System.out.println("Waked Up! Crying wuwuwuwu……");
        cry = true;
        WakeUpEvent wakeUpEvent=new WakeUpEvent(System.currentTimeMillis(),"bed",this);
        for (MyObserver observer : observers) {
            observer.actionOnWakeUp(wakeUpEvent);
        }
    }
}

//发出 事件类
class WakeUpEvent {
    long timesTame;//时间
    String loc;//地点
    private Child source;

    public WakeUpEvent(long timesTame, String loc, Child child) {
        this.timesTame = timesTame;
        this.loc = loc;
        this.source = child;
    }
}

interface MyObserver {
    void actionOnWakeUp(WakeUpEvent wakeUpEvent);
}

class Dad implements MyObserver {
    public void feed() {
        System.out.println("dad feeding");
    }

    @Override
    public void actionOnWakeUp(WakeUpEvent wakeUpEvent) {
        feed();
    }
}

class Mum implements MyObserver {
    public void hug() {
        System.out.println("dad feeding");
    }

    @Override
    public void actionOnWakeUp(WakeUpEvent wakeUpEvent) {
        hug();
    }
}

class Dog implements MyObserver {
    public void wang() {
        System.out.println("dad feeding");
    }

    @Override
    public void actionOnWakeUp(WakeUpEvent wakeUpEvent) {
        wang();
    }
}


public class Main {
    public static void main(String[] args) {
        Child child = new Child();
        child.wakeUp();
    }
}
